﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tycho.Runtime;
using System.Windows.Controls;
using System.Windows;

namespace Tycho.Peyote {
    public abstract class GlyphPresenter {
        public AnyObject Pattern { get; private set; }
        public string DisplayName { get; private set; }

        public GlyphPresenter (string displayName, AnyObject pattern) {
            DisplayName = displayName;
            Pattern = pattern;
        }

        public GlyphPresenter (string displayName) {
            DisplayName = displayName;
            Pattern = null;
        }

        public virtual bool Match (AnyObject obj) {
            return Pattern.Match (RuntimeModule.Null, obj);
        }

        public abstract UIElement Present (AnyObject obj, Environment environment);
    }

    public class GlyphPresenterRegistry {
        public List<GlyphPresenter> Presenters { get; private set; }
        public Environment Environment { get; private set; }

        public GlyphPresenterRegistry (Environment environment) {
            Presenters = new List<GlyphPresenter> ();
            Environment = environment;
        }

        public UIElement Present (AnyObject obj) {
            foreach (GlyphPresenter presenter in Presenters) {
                if (presenter.Match (obj)) {
                    return presenter.Present (obj, Environment);
                }
            }

            return DefaultPresentation (obj);
        }

        public UIElement DefaultPresentation (AnyObject obj) {
            TextBox textBox = new TextBox ();
            textBox.IsReadOnly = true;
            textBox.Text = obj.ToString ();
            textBox.BorderThickness = new Thickness (0);
            return textBox;
        }
    }
}
